package com.wzh.himalaya.views;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wzh.himalaya.PlayerActivity;
import com.wzh.himalaya.R;
import com.wzh.himalaya.adapters.PlayListAdapter;
import com.wzh.himalaya.base.BaseApplication;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;

import java.util.List;

/**
 * className:PopWindow
 * Author:WZH
 * Time:2022-05-09
 */
public class PopWindow extends PopupWindow {

    private final View mPopView;
    private View mCloseBtn;
    private RecyclerView mTrackList;
    private PlayListAdapter mPlayListAdapter;
    private TextView mPlayModeTv;
    private ImageView mPlayModeIv;
    private View mPlayModeContainer;
    private PlayListActionClickListener mPlayModeClickListener=null;
    private PlayerActivity mPlayerActivity;
    private View mOrderBtnContainer;
    private ImageView mOrderIcon;
    private TextView mOrderText;

    public PopWindow(){
        //因为view不能先载入，所以把super放到view的载入之前。 只先设置宽高
        super( ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        //view的外部可以点击
        setOutsideTouchable(true);

        //载入view
        mPopView = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.pop_play_list,null);
        //设置内容
        setContentView(mPopView);
        //设置窗口进入和退出的动画
        setAnimationStyle(R.style.pop_animation);
        
        initView();
        initEvent();

    }

    private void initEvent() {
        //点击关闭以后 窗口消失
        mCloseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭 这是个popWindow的方法
                dismiss();
            }
        });

        mPlayModeContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo:切换播放模式
                if (mPlayModeClickListener != null) {
                    mPlayModeClickListener.onPlayModeClick();
                }

            }
        });

        mOrderBtnContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //todo:切换播放列表的正序或者逆序
                mPlayModeClickListener.onOrderClick();

            }
        });

    }

    private void initView() {
        mCloseBtn = mPopView.findViewById(R.id.play_list_close_bn);
        mTrackList = mPopView.findViewById(R.id.play_list_rv);
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseApplication.getAppContext());
        mTrackList.setLayoutManager(layoutManager);
        //设置适配器
        mPlayListAdapter = new PlayListAdapter();
        mTrackList.setAdapter(mPlayListAdapter);

        //播放模式
        mPlayModeTv = mPopView.findViewById(R.id.play_list_play_mode_tv);
        mPlayModeIv = mPopView.findViewById(R.id.play_list_play_mode_iv);
        mPlayModeContainer = mPopView.findViewById(R.id.play_list_mode_container);
        //
        mOrderBtnContainer = mPopView.findViewById(R.id.play_list_order_container);
        mOrderIcon = mPopView.findViewById(R.id.play_list_order_iv);
        mOrderText = mPopView.findViewById(R.id.play_list_order_tv);

    }

    public void setListData(List<Track> data){
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setData(data);
        }
    }

    public void setCurrentPlayPosition(int position){
        //设置播放列表里的当前播放位置
        if (mPlayListAdapter != null) {
            mPlayListAdapter.setCurrentPlayPosition(position);
        }

        mTrackList.scrollToPosition(position);//这个是设置列表就是以第几个position开始显示
    }



    public void setPlayItemClickListener(playListItemClickListener listener) {
       mPlayListAdapter.setOnItemClickListener(listener);
    }

    /**
     * 更新播放列表里的播放模式
     * @param currentMode
     */
    public void updatePlayMode(XmPlayListControl.PlayMode currentMode) {
        updatePlayModeBtnImg(currentMode);
    }

    //更新切换正逆序
    public void updateOrderIcon(boolean isOrder){
        mOrderIcon.setImageResource(isOrder?R.mipmap.play_order_recverse:R.mipmap.mode_music);
        mOrderText.setText(isOrder?"逆序":"正序");
    }


    //1、默认的是：PLAY_MODEL_LIST
    //2、点击的时候切换到列表循环 PLAY_MODEL_LIST_LOOP
    //3、随机播放 PLAY_MODEL_RANDOM
    //4、单曲循环 PLAY_MODEL_SINGLE_LOOP
    private void updatePlayModeBtnImg(XmPlayListControl.PlayMode playMode) {
        int resId = R.mipmap.sort_descending;
        int textId = R.string.play_mode_order_text;
        switch (playMode){
            case PLAY_MODEL_LIST:
                resId = R.mipmap.sort_descending;
                textId=R.string.play_mode_order_text;
                break;
            case PLAY_MODEL_LIST_LOOP:
                resId = R.mipmap.play_mode_loop_normal;
                textId=R.string.play_mode_random_text;
                break;
            case PLAY_MODEL_RANDOM:
                resId = R.mipmap.play_mode_random_normal;
                textId=R.string.play_mode_list_play_text;
                break;
            case PLAY_MODEL_SINGLE_LOOP:
                resId = R.mipmap.play_mode_loop_one_normal;
                textId=R.string.play_mode_single_play_text;
                break;
        }
        mPlayModeIv.setImageResource(resId);
        mPlayModeTv.setText(textId);
    }




    public interface playListItemClickListener{
        void onItemClick(int position);
    }




    public void setPlayListActionClickListener(PlayListActionClickListener PlayModeClickListener){
        mPlayModeClickListener = PlayModeClickListener;
    }

    public interface PlayListActionClickListener {
        //播放模式被点击了
        void onPlayModeClick();

        //正逆序被点击
        void onOrderClick();
    }
}
